package cn.zz.veggie.web.controller.mgmt;

import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.comm.core.types.result.PageResult;
import cn.zz.comm.web.annotation.BoxRespResult;
import cn.zz.veggie.application.service.OrderAppService;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderAppendDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderCreateDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderItemRemoveDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderItemSaveDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderQueryDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderSaveDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderStatusDto;
import cn.zz.veggie.web.types.vo.mgmt.MgmtOrderDetailVo;
import cn.zz.veggie.web.types.vo.mgmt.MgmtOrderVo;
import cn.zz.veggie.web.types.vo.mgmt.order.MgmtOrderBriefVo;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;

/**
 * @author zhongq
 * @since 2024/4/25
 */
@Validated
@BoxRespResult
@RestController
@RequiredArgsConstructor
@RequestMapping("/mgmt/order")
public class MgmtOrderController {

	private final OrderAppService orderAppService;

	/**
	 * 订单列表
	 */
	@GetMapping("/list")
	public PageResult<MgmtOrderVo> list(@Validated MgmtOrderQueryDto dto,
			@RequestParam(defaultValue = "1") @Positive Integer current,
			@RequestParam(defaultValue = "10") @Positive Integer size) {
		return orderAppService.adminGetList(dto, PageQuery.of(current, size));
	}
	/**
	 * 保存订单
	 */
	 @PostMapping("/save")
	 public void saveOrder(@Validated @RequestBody MgmtOrderSaveDto dto ){
		 orderAppService.mgmtSaveOrder(dto);
	 }

	/**
	 * 最近订单
	 */
	@GetMapping("/recent")
	public List<MgmtOrderBriefVo> recent(@RequestParam(defaultValue = "10") @Positive Integer size) {
		return orderAppService.mgmtGetRecent(size);
	}

	/**
	 * 删除订单
	 */
	@DeleteMapping("/remove")
	public void remove(@Positive Integer id) {
		orderAppService.mgmtRemoveOrder(id);
	}

	/**
	 * 订单详情
	 */
	@GetMapping("/detail")
	public MgmtOrderDetailVo detail(@RequestParam @Positive Integer id) {
		return orderAppService.adminGetDetail(id);
	}

	/**
	 * 修改订单状态
	 */
	@PutMapping("/status")
	public void changeStatus(@RequestBody @Validated MgmtOrderStatusDto dto) {
		orderAppService.adminChangeStatus(dto);
	}

	/**
	 * 重算价格
	 */
	@PutMapping("/re-calc-amount")
	public void refreshAmount(@RequestParam @Positive Integer id) {
		orderAppService.adminReCalcAmount(id);
	}

	/**
	 * 创建订单
	 */
	@PostMapping("/create")
	public void create(@RequestBody @Validated MgmtOrderCreateDto dto) {
		orderAppService.adminCreate(dto);
	}

	/**
	 * 添加/修改订单项
	 */
	@PostMapping("/item")
	public void item(@RequestBody @Validated MgmtOrderItemSaveDto dto) {
		orderAppService.adminSaveOrderItem(dto);
	}

	/**
	 * 添加到订单
	 */
	@PostMapping("/item/append")
	public void append(@RequestBody @Validated MgmtOrderAppendDto dto) {
		orderAppService.adminOrderAppend(dto);
	}

	@DeleteMapping("/item/remove")
	public void itemRemove(@Validated MgmtOrderItemRemoveDto dto) {
		orderAppService.mgmtItemRemove(dto);
	}

}